package com.coffee.admin.service.platform.impl;

import com.coffee.admin.api.platform.query.CarouselFigureQuery;
import com.coffee.admin.api.platform.result.CarouselFigureResult;
import com.coffee.admin.api.platform.service.CarouselFigureService;
import com.coffee.admin.service.platform.bean.CarouselFigure;
import com.coffee.admin.service.platform.dao.CarouselFigureDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.exception.ValidateBusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class CarouselFigureServiceImpl implements CarouselFigureService {
    @Value("${cityId}")
    private String nationwideId;
    @Resource
    private CarouselFigureDao carouselFigureDao;

    @Override
    public Page<CarouselFigureResult> getPager(CarouselFigureQuery param, PagerInfo pr) {
        Page<CarouselFigureResult> pager = new Page<CarouselFigureResult>(pr.getStart(), pr.getPageSize());
        Integer count = carouselFigureDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<CarouselFigure> list = carouselFigureDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, CarouselFigureResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(CarouselFigureQuery param) {
        Integer count = carouselFigureDao.count(param);
        return count;
    }

    @Override
    public CarouselFigureResult getDetail(Integer id) {
        CarouselFigure item = carouselFigureDao.findById(id);
        if (item == null) {
            return null;
        }
        CarouselFigureResult result = new CarouselFigureResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<CarouselFigureResult> getList(CarouselFigureQuery param, PagerInfo pr) {
        List<CarouselFigure> list = carouselFigureDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, CarouselFigureResult.class);
    }

    @Override
    public List<CarouselFigureResult> getList(CarouselFigureQuery param) {
        Integer cityId = param.getCityId();
        if (param.getCityId() == null) {
            cityId = Integer.parseInt(nationwideId);
        }
        param.setCityId(cityId);
        List<CarouselFigure> carouselFigures = carouselFigureDao.pageByCity(param, 0, Integer.MAX_VALUE);
        if (carouselFigures == null || carouselFigures.size() <= 0) {
            param.setCityId(Integer.parseInt(nationwideId));
            carouselFigures = carouselFigureDao.pageByCity(param, 0, Integer.MAX_VALUE);
        }
        return DTOUtils.convertList(carouselFigures, CarouselFigureResult.class);

    }

    @Override
    public List<CarouselFigureResult> getList() {
        CarouselFigureQuery param = new CarouselFigureQuery();
        List<CarouselFigure> list = carouselFigureDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, CarouselFigureResult.class);
    }


    @Override
    public void delete(Integer id) {
        carouselFigureDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(CarouselFigureResult result) throws BusinessException {
        CarouselFigure item = new CarouselFigure();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            item.setCreateTime(new Date());
            item.setIsEnable("N");
            carouselFigureDao.insert(item);
        } else {
            CarouselFigure tmp = carouselFigureDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            if ("Y".equals(tmp.getIsEnable())) {
                throw new BusinessException(ExceptionEnum.ERROR_1315.getCode(), ExceptionEnum.ERROR_1315.getMessage());
            }
            carouselFigureDao.updateById(item);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCarouselFigureCity(List<Integer> ids, Integer cityId) throws BusinessException {
        List<Integer> carouselFigureId = carouselFigureDao.findCarouselFigureId(cityId);
        carouselFigureDao.deleteByIdAndCityId(null, cityId);
        for (Integer integer : carouselFigureId) {
            Integer byIdAndCityId = carouselFigureDao.findByIdAndCityId(integer, null);
            if (byIdAndCityId == 0) {
                CarouselFigure carouselFigure = new CarouselFigure();
                carouselFigure.setId(integer);
                carouselFigure.setIsEnable("N");
                carouselFigureDao.updateById(carouselFigure);
            }
        }
        for (Integer id : ids) {
            carouselFigureDao.insertCity(id, cityId);
            CarouselFigure carouselFigure = new CarouselFigure();
            carouselFigure.setId(id);
            carouselFigure.setIsEnable("Y");
            carouselFigureDao.updateById(carouselFigure);
        }
    }

    @Override
    public void deleteCarouselFigureCity(Integer id, Integer cityId) throws BusinessException {
        carouselFigureDao.deleteByIdAndCityId(id, cityId);
    }

    @Override
    public void carouselEnable(String ids) throws BusinessException {
        ValidateBusinessException.assertStringNotBlank(ids, ExceptionEnum.ERROR_1316);
        carouselFigureDao.updateIsEnable();
        List<Integer> idList = Arrays.stream(StringUtils.split(ids, ","))
                .map(Integer::valueOf).distinct().collect(Collectors.toList());
        for (int i = 0; i < idList.size(); i++) {
            CarouselFigure param = new CarouselFigure();
            param.setId(idList.get(i));
            param.setIsEnable("Y");
            param.setSort(i);
            carouselFigureDao.updateById(param);
        }
    }

    @Override
    public Map<Integer, CarouselFigureResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, CarouselFigureResult> map = new HashMap<Integer, CarouselFigureResult>();
        List<CarouselFigure> list = carouselFigureDao.findByIds(StringUtil.setToList(idSet));
        List<CarouselFigureResult> resultList = DTOUtils.convertList(list, CarouselFigureResult.class);
        for (CarouselFigureResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, CarouselFigureResult> getItemMap() {
        Map<Integer, CarouselFigureResult> map = new HashMap<Integer, CarouselFigureResult>();
        CarouselFigureQuery param = new CarouselFigureQuery();
        List<CarouselFigure> list = carouselFigureDao.page(param, 0, Integer.MAX_VALUE);
        List<CarouselFigureResult> resultList = DTOUtils.convertList(list, CarouselFigureResult.class);
        for (CarouselFigureResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
